<?php
/**
 * Created by PhpStorm.
 * DateTime: 2020/11/3 9:06
 * Intro:
 */

namespace addons\podunk\controller;


use think\Exception;

class DeviceGroup extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        return parent::_initialize();
    }

    public function getList(){
        $search = $this->request->get('search');

        $pageSize = $this->request->get('pageSize')?$this->request->get('pageSize'):10;


        $this->success('设备分组列表', \addons\podunk\model\DeviceGroup::getDeviceGroupList($search, $pageSize));
    }

    public function add(){
        $params = $this->request->post();
        $this->podunkValidate($params, get_class(), 'add');

        try{
            \addons\podunk\model\DeviceGroup::create([
                'user_id' => $this->auth->id,
                'name' => $params['name']
            ]);
            $this->success('添加设备分组成功');
        }catch (Exception $e){
            $this->error($e->getMessage());
        }

    }

    public function edit(){
        $params = $this->request->post();
        $this->podunkValidate($params, get_class(), 'edit');

        try{
            $deviceGroup = \addons\podunk\model\DeviceGroup::get($params['id']);
            $deviceGroup->name = $params['name'];
            $deviceGroup->save();
            $this->success('更新设备分组成功');
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function del(){
        $params = $this->request->post();
        $this->podunkValidate($params, get_class(), 'del');

        try{
            $deviceGroup = \addons\podunk\model\DeviceGroup::get($params['id']);
            $deviceGroup->delete();
            $this->success('删除设备分组成功');
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }


}